// pages/createRoom/createRoom.js
import {$await} from '../../utils/util'
import {audioRoomAPI} from '../../api/audioRoom/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notice:'',// 公告
    topic:'', // 话题
    createInfo:{ // 房间信息
      topic_end:'',
      topic_start:'',
    },
    hasTime:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.room_number = options.room_number
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 更改房间信息
  async updateInfo(){
    if(!this.data.topic){
      return wx.showToast({
        icon:'none',
        title: '请填写房间话题',
        duration: 2000
      })
    }
    const {topic,notice} = this.data
    const params = {
      ...this.data.createInfo,
      notice,
      topic,
      room_number:this.room_number,
    }
    const [err,res] = await $await(audioRoomAPI.roomInfo({...params}))
    if(res){
      wx.showToast({
        icon:'none',
        title: '修改成功',
        duration: 2000
      })
      wx.redirectTo({
        url:`/pages/roomManage/roomManage?roomNumber=${this.room_number}`,
      })
    }
  },
  // 时间是否选择
  toggleTime(){
    this.setData({hasTime:!this.data.hasTime})
  },
  // 协议是否同意
  toggleAgree(){
    this.setData({agree:!this.data.agree})
  },
  // 选择开始时间
  startTimeChange(e){
    const {detail:{value:start}} = e
    const topic_start = 'createInfo.topic_start'
    this.setData({[topic_start]:start})
  },
  // 选择结束时间
  endTimeChange(e){
    const {detail:{value:end}} = e
    const topic_end = 'createInfo.topic_end'
    this.setData({[topic_end]:end})
  },
  
})